﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.IO;
using System.Drawing;

namespace Guoc.BigMall.Infrastructure.File
{
    public class FileUploadService : IFileUpload
    {
        /// <summary>
        ///  保存图片
        /// </summary>
        /// <param name="base64Image"></param>
        /// <param name="saveDic">图片存储目录</param>
        /// <param name="saveDic">文件名</param>
        public void UploadImage(string base64Image, string saveDic,string fileName)
        {
            try
            {
                //图片Base64编码转换成Image对象
               string imgCode = Regex.Replace(base64Image, "data.*base64,", "", RegexOptions.IgnoreCase);
               byte[] imageBytes = Convert.FromBase64String(imgCode);
               using (MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
                {
                   
                    Bitmap bmp = new Bitmap(ms);
                    if (!Directory.Exists(saveDic))
                    {                       
                        Directory.CreateDirectory(saveDic);
                    }
                    var savePath = Path.Combine(saveDic, fileName);
                    bmp.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
                }
                
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
